<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\Banner;
use app\common\model\Config;
use app\common\services\UrlService;         // url
use app\common\services\CourseService;      // 课程


/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['index'];
    protected $noNeedRight = ['*'];

    /**
     * 首页接口
     */
    public function index()
    {
        $result['banner']    = $this->getBanner();
        $result['recommend'] = $this->recommend($this->auth->id);
        $this->success('success',$result);
    }


    /**
     * 获取首页banner
     *
     * @return void
     */
    protected function getBanner(){

        $where['show_switch'] = 1;
        $where['pos'] = 'INDEX_TOP';
        $fields = "id,title,type,url,images";
        $result = Banner::where($where)
                ->field($fields)
                ->select();

        if(!empty($result)){
            foreach($result as &$value){

                //  url 路径拼接
                $value['url']    = UrlService::getFileUrl($value['url']);
                $value['images'] = UrlService::getFileUrl($value['images']);
                unset($value);
            }
        }
        return $result ?? [];
    }



    public function aboutus()
    {
        $data = Config::where('name','about_us')->value('value');
        return $this->success('success',$data);
    }


    /**
     * 推荐课程
     *
     * @param $user
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function recommend($user){

        $list = CourseService::getHotList($user);
        return $list;
    }
}
